/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hunterxhunter.greedisland.gui;

import hunterxhunter.greedisland.GreedIslandGame;
import hunterxhunter.greedisland.data.enums.Collision;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;
import framework.eea.gui.JFrameGameContainer;
import hunterxhunter.greedisland.data.map.LayerType;
import hunterxhunter.greedisland.data.map.Tile;

/**
 *
 * @author otaku
 */
public class EditorFrame extends javax.swing.JFrame {

    private JFrameGameContainer gc;
    private GreedIslandGame game;
    private Point sourceLocation;
    
    /**
     * Creates new form EditorFrame
     */
    public EditorFrame(GreedIslandGame game, JFrameGameContainer gc) {
        initComponents();
        
        this.gc = gc;
        this.game = game;
        
        List<Entry<String, Image>> images = new ArrayList<Entry<String, Image>>(gc.getAllImages());
        String[] names = new String[images.size()];
        for(int i = 0; i < names.length; i++) {
            names[i] = images.get(i).getKey();
        }
        Arrays.sort(names);
        jListImage.setListData(names);
        jListImage.setSelectedValue("/greedisland/img/map/rpgmaker.png", true);
        
        sourceLocation = new Point(0, 256);
        
        jSplitPaneMain.setDividerLocation(1.0);
    }

    public LayerType getLayerType() {
        if(jRadioButtonAOT.isSelected()) {
            return LayerType.ALWAYS_ON_TOP;
        } else if(jRadioButtonFG.isSelected()) {
            return LayerType.FOREGROUND;
        } else if(jRadioButtonBG.isSelected()) {
            return LayerType.BACKGROUND;
        }
        return null;
    }
    
    public Collision getCollision() {
        if(jRadioButtonALL.isSelected()) {
            return Collision.ALL;
        } else if(jRadioButtonRU.isSelected()) {
            return Collision.RU;
        } else if(jRadioButtonRD.isSelected()) {
            return Collision.RD;
        } else if(jRadioButtonLU.isSelected()) {
            return Collision.LU;
        } else if(jRadioButtonLD.isSelected()) {
            return Collision.LD;
        } else if(jRadioButtonNO.isSelected()) {
            return Collision.NO;
        }
        return Collision.NO;
    }
    
    public Rectangle getSourceRect() {
        return new Rectangle(sourceLocation, new Dimension(jSliderSize.getValue(), jSliderSize.getValue()));
    }
    
    public String getImageRef() {
        return (String) jListImage.getSelectedValue();
    }
    
    private void panelPaint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        if(jListImage.getSelectedValue() != null) {
            g.drawImage(gc.getImage((String)jListImage.getSelectedValue()), 0, 0, null);
            g2d.setStroke(new BasicStroke(2));
            g.setColor(Color.black);
            Rectangle rect = getSourceRect();
            g.drawRect(rect.x, rect.y, rect.width, rect.height);
        }
    }
    
    public void updateSettings(Tile tile) {
        LayerType t = getLayerType();
        if(tile.hasLayerType(t)) {
            sourceLocation.setLocation(tile.getSrc(t).getLocation());
            switch(tile.getCollision()) {
                case ALL: jRadioButtonALL.setSelected(true); break;
                case RU: jRadioButtonRU.setSelected(true); break;
                case RD: jRadioButtonRD.setSelected(true); break;
                case LU: jRadioButtonLU.setSelected(true); break;
                case LD: jRadioButtonLD.setSelected(true); break;
                case NO: jRadioButtonNO.setSelected(true); break;
            }
            jListImage.setSelectedValue(tile.getImageRef(t), true);
            jPanelImage.updateUI();
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroupCollison = new javax.swing.ButtonGroup();
        buttonGroupLayer = new javax.swing.ButtonGroup();
        jSplitPaneMain = new javax.swing.JSplitPane();
        jPanelImage = new javax.swing.JPanel() {
            @Override
            public void paint(Graphics g) {
                super.paint(g);
                panelPaint(g);
            }
        };
        jPanelImageOption = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListImage = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        jRadioButtonLD = new javax.swing.JRadioButton();
        jRadioButtonRU = new javax.swing.JRadioButton();
        jRadioButtonRD = new javax.swing.JRadioButton();
        jRadioButtonLU = new javax.swing.JRadioButton();
        jRadioButtonNO = new javax.swing.JRadioButton();
        jRadioButtonALL = new javax.swing.JRadioButton();
        jSliderSize = new javax.swing.JSlider();
        jRadioButtonAOT = new javax.swing.JRadioButton();
        jRadioButtonFG = new javax.swing.JRadioButton();
        jRadioButtonBG = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Editor");

        jSplitPaneMain.setResizeWeight(1.0);

        jPanelImage.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jPanelImageMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanelImageLayout = new javax.swing.GroupLayout(jPanelImage);
        jPanelImage.setLayout(jPanelImageLayout);
        jPanelImageLayout.setHorizontalGroup(
            jPanelImageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanelImageLayout.setVerticalGroup(
            jPanelImageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 498, Short.MAX_VALUE)
        );

        jSplitPaneMain.setLeftComponent(jPanelImage);

        jListImage.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jListImage.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jListImageMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jListImage);

        buttonGroupCollison.add(jRadioButtonLD);
        jRadioButtonLD.setText("└");

        buttonGroupCollison.add(jRadioButtonRU);
        jRadioButtonRU.setText("┐");
        jRadioButtonRU.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jRadioButtonRU.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);

        buttonGroupCollison.add(jRadioButtonRD);
        jRadioButtonRD.setText("┘");
        jRadioButtonRD.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jRadioButtonRD.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);

        buttonGroupCollison.add(jRadioButtonLU);
        jRadioButtonLU.setText("┌");
        jRadioButtonLU.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);

        buttonGroupCollison.add(jRadioButtonNO);
        jRadioButtonNO.setSelected(true);
        jRadioButtonNO.setText("No Collision");

        buttonGroupCollison.add(jRadioButtonALL);

        jSliderSize.setMaximum(128);
        jSliderSize.setMinimum(8);
        jSliderSize.setMinorTickSpacing(8);
        jSliderSize.setPaintLabels(true);
        jSliderSize.setPaintTicks(true);
        jSliderSize.setSnapToTicks(true);
        jSliderSize.setValue(32);
        jSliderSize.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderSizeStateChanged(evt);
            }
        });

        buttonGroupLayer.add(jRadioButtonAOT);
        jRadioButtonAOT.setText("Always on Top");

        buttonGroupLayer.add(jRadioButtonFG);
        jRadioButtonFG.setText("Foreground");

        buttonGroupLayer.add(jRadioButtonBG);
        jRadioButtonBG.setSelected(true);
        jRadioButtonBG.setText("Background");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jSliderSize, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButtonLU)
                            .addComponent(jRadioButtonLD))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButtonALL)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButtonRD)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jRadioButtonRU)
                                .addGap(18, 18, 18)
                                .addComponent(jRadioButtonNO)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButtonAOT)
                    .addComponent(jRadioButtonFG)
                    .addComponent(jRadioButtonBG))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(jRadioButtonALL)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSliderSize, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jRadioButtonRU)
                            .addComponent(jRadioButtonLU)
                            .addComponent(jRadioButtonNO)
                            .addComponent(jRadioButtonAOT))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jRadioButtonLD)
                                .addComponent(jRadioButtonRD))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(16, 16, 16)
                                .addComponent(jRadioButtonFG)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButtonBG)
                        .addContainerGap(18, Short.MAX_VALUE))))
        );

        javax.swing.GroupLayout jPanelImageOptionLayout = new javax.swing.GroupLayout(jPanelImageOption);
        jPanelImageOption.setLayout(jPanelImageOptionLayout);
        jPanelImageOptionLayout.setHorizontalGroup(
            jPanelImageOptionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1)
        );
        jPanelImageOptionLayout.setVerticalGroup(
            jPanelImageOptionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelImageOptionLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jSplitPaneMain.setRightComponent(jPanelImageOption);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPaneMain)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPaneMain)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jListImageMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jListImageMouseClicked
        jPanelImage.updateUI();
    }//GEN-LAST:event_jListImageMouseClicked

    private void jPanelImageMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanelImageMouseClicked
        int x = evt.getX() / jSliderSize.getValue();
        int y = evt.getY() / jSliderSize.getValue();
        sourceLocation.x = x * jSliderSize.getValue();
        sourceLocation.y = y * jSliderSize.getValue();
        jPanelImage.updateUI();
    }//GEN-LAST:event_jPanelImageMouseClicked

    private void jSliderSizeStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderSizeStateChanged
        jPanelImage.updateUI();
    }//GEN-LAST:event_jSliderSizeStateChanged

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroupCollison;
    private javax.swing.ButtonGroup buttonGroupLayer;
    private javax.swing.JList jListImage;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanelImage;
    private javax.swing.JPanel jPanelImageOption;
    private javax.swing.JRadioButton jRadioButtonALL;
    private javax.swing.JRadioButton jRadioButtonAOT;
    private javax.swing.JRadioButton jRadioButtonBG;
    private javax.swing.JRadioButton jRadioButtonFG;
    private javax.swing.JRadioButton jRadioButtonLD;
    private javax.swing.JRadioButton jRadioButtonLU;
    private javax.swing.JRadioButton jRadioButtonNO;
    private javax.swing.JRadioButton jRadioButtonRD;
    private javax.swing.JRadioButton jRadioButtonRU;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSlider jSliderSize;
    private javax.swing.JSplitPane jSplitPaneMain;
    // End of variables declaration//GEN-END:variables
}
